module.exports = (option, app) => {
    return async (ctx, next) => {
        // const { token } = ctx.header
  
        let token=ctx.header.token||ctx.query.token;
        console.log(token, "token")
        if (!token) {
            ctx.throw(400, "你没有权限访问该接口")
        }
        let user = {};
        try {
            user = ctx.checkToken(token)

        } catch (error) {

            console.log(error.name, "error")
            let fail = error.name === "TokenExpiredError"
                ? "token 已过期! 请重新获取令牌"
                : "Token 令牌不合法!";
            ctx.throw(401, fail)
        }
         console.log(user,"user")
        let t = await ctx.service.cache.get("user_" + user.id)
  
        if (!t || t !== token) {
            ctx.throw(401, "Token1 令牌不合法!")
        }
        console.log(ctx ,"ctx.request.body.type ")
        if(ctx.request.body.type == 'mobile'||ctx.header.type == 'mobile'){
            user = await app.model.User.findByPk(user.id);
       
        }else{
            user = await app.model.Manager.findByPk(user.id);
       
        }
        if (!user) {
            ctx.throw(400,"用户不存在或已被禁用")
        }
        ctx.authUser= user;
        console.log(ctx.authUser,"authuser")
        await next()
    }

}